﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var sql =string.Format("select * from Articles where Author like '%{0}%'", name);
            var sup = Dbhelp.GetData(sql);
            dataGridView1.DataSource = sup; 
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            var sql = " select * from Articles";
            var sup = Dbhelp.GetData(sql);
            dataGridView1.DataSource = sup;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Addition addition = new Addition();
            var res = addition.ShowDialog();
            if (res == DialogResult.Yes )
            {
                var name = textBox1.Text;
                var sqlstu = string.Format("select * from Articles where Author like '%{0}%'", name);
                var sup = Dbhelp.GetData(sqlstu);
                dataGridView1.DataSource = sup;
            }
            else
            {
                MessageBox.Show("失败！");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var Title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var Author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var CreatedTime = dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value.ToString();
                Addition addition = new Addition(id, Title, Author, CreatedTime);
                var res = addition.ShowDialog();
                if (res == DialogResult.Yes)
                {
                    var name = textBox1.Text;
                    var sqlstu = string.Format("select * from Articles where Author like '%{0}%'", name);
                    var sup = Dbhelp.GetData(sqlstu);
                    dataGridView1.DataSource = sup;
                }
                else
                {
                    MessageBox.Show("失败！");
                }
            }
            else
            {
                MessageBox.Show("先选择行");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var de = dataGridView1.SelectedRows[0];
                var id = (int)de.Cells["Id"].Value;
                var Deleteprompt = MessageBox.Show("你确定要删除？" , "提示", MessageBoxButtons.YesNo , MessageBoxIcon.Information);
                if (Deleteprompt.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where Id={0}", id);
                    var nwwass = Dbhelp.AddorUpdateOrDelete(sql);
                    if (nwwass > 0)
                    {
                        var name = textBox1.Text;
                        var sqlstu = string.Format("select * from Articles where Author like '%{0}%'", name);
                        var dt = Dbhelp.GetData(sqlstu);
                        dataGridView1.DataSource = dt;
                        MessageBox.Show("删除成功");
                    }
                }
            }
            else
            {
                MessageBox.Show("你没选择行，请选择一条行");
            }
        }
    }
}
